﻿
// test_lzma.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "LzmaLib.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <iomanip>
#include <string>

#define LZMA_PROPS_SIZE 5

using namespace std;
using namespace cv;

typedef unsigned int __u32;
typedef unsigned char __u8;
unsigned char dest[10240] = { 0 };
unsigned char src[10240] = { 1 };
unsigned char src2[10240] = { 0 };

typedef struct __fb_file_header
{
	__u32 version;
	__u32 magic_id;
	__u32 height;
	__u32 width;
	__u32 fmt_type;
	__u32 cs_mode;
	__u32 pix_fmt;
	__u32 pix_seq;
	__u32 mod_or_br_swap;
	__u32 data_len[3];
	__u32 offset_data[3];
	__u32 reserve[6];
} __fb_file_header_t;

typedef struct _CLzmaEncProps
{
	int level;       /*  0 <= level <= 9 */
	UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
						(1 << 12) <= dictSize <= (1 << 30) for 64-bit version
						 default = (1 << 24) */
	int lc;          /* 0 <= lc <= 8, default = 3 */
	int lp;          /* 0 <= lp <= 4, default = 0 */
	int pb;          /* 0 <= pb <= 4, default = 2 */
	int algo;        /* 0 - fast, 1 - normal, default = 1 */
	int fb;          /* 5 <= fb <= 273, default = 32 */
	int btMode;      /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
	int numHashBytes; /* 2, 3 or 4, default = 4 */
	UInt32 mc;        /* 1 <= mc <= (1 << 30), default = 32 */
	unsigned writeEndMark;  /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
	int numThreads;  /* 1 or 2, default = 2 */
} CLzmaEncProps;

void RGB2YUV(unsigned char r, unsigned char g, unsigned char b, unsigned char &y, unsigned char &u, unsigned char &v)
{
	y = 0.299 * r + 0.587 * g + 0.114 * b;
	u = -0.1687 * r - 0.3313 * g + 0.5 * b + 128;
	v = 0.5 * r - 0.4187 * g - 0.0813 * b + 128;
}

void RGB24TOYUV422(unsigned char *pRgb, int w, int h, unsigned char *pYuv)
{
	w = w / 2 * 2;

	for (int height = 0; height < h; height++)
	{
		unsigned char *ptrRGB = pRgb + height * w * 3;
		unsigned char *ptrYUV = pYuv + height * w * 2;

		for (int width = 0; width < w; width += 2)
		{
			unsigned char y1, u1, v1, y2, u2, v2, r1, g1, b1, r2, g2, b2;
			r1 = *ptrRGB;
			ptrRGB++;
			g1 = *ptrRGB;
			ptrRGB++;
			b1 = *ptrRGB;
			ptrRGB++;

			r2 = *ptrRGB;
			ptrRGB++;
			g2 = *ptrRGB;
			ptrRGB++;
			b2 = *ptrRGB;
			ptrRGB++;

			RGB2YUV(r1, g1, b1, y1, u1, v1);
			RGB2YUV(r2, g2, b2, y2, u2, v2);

			//u、v分量取平均值
			unsigned char u = (u1 + u2) / 2;
			unsigned char v = (v1 + v2) / 2;
			*ptrYUV = y1;
			ptrYUV++;
			*ptrYUV = u;
			ptrYUV++;
			*ptrYUV = y2;
			ptrYUV++;
			*ptrYUV = v;
			ptrYUV++;
		}
	}
}

static uint32_t checkSum(uint8_t *data, uint32_t len)
{
	uint32_t res = 0;
	uint32_t k = 0;
	while (k < len)
	{
		res ^= data[k];
		k++;
	}
	return res;
}

int convertPng2Yuyv(const char* filename)
{
	string fn(filename);
	size_t extPos = fn.find(".png");
	string extName = fn.substr(extPos);
	string pureName = fn.substr(0, fn.size() - extName.size());

	string opFn = pureName + ".yuyv";

	//FILE *bmp_fd = NULL;
	//fopen_s(&bmp_fd, filename, "rb");
	//fseek(bmp_fd, 0, SEEK_END);
	//long bmp_size = ftell(bmp_fd);
	//fseek(bmp_fd, 0, SEEK_SET);
	//__u8 * pbmp = (__u8*)malloc(bmp_size);
	//fread_s(pbmp, bmp_size, bmp_size, 1, bmp_fd);
	//fclose(bmp_fd);
	Mat image = imread(filename);
	if (image.empty()) {
		std::cout << "Failed to load image file." << std::endl;
		return 1;
	}
	//// 2.构造rgb数据空间
	size_t bmp_size = image.rows * image.cols * 3;
	printf("bmp_size:%d\n", bmp_size);
	__u8 * pbmp = (__u8*)malloc(bmp_size);
	int k = 0;
	for (int row = 0; row < image.rows; row++) {
		for (int col = 0; col < image.cols; col++) {
			cv::Vec3b pixel = image.at<cv::Vec3b>(row, col);
			__u8 r = pixel[2];
			__u8 g = pixel[1];
			__u8 b = pixel[0];
			pbmp[k] = r;
			pbmp[k + 1] = g;
			pbmp[k + 2] = b;
			k += 3;
			//pbmp[3 * row * image.rows + 3 * col] = r;
			//pbmp[3 * row * image.rows + 3 * col + 1] = g;
			//pbmp[3 * row * image.rows + 3 * col + 2] = b;
		}
	}
	//// 3.构建yuyv存储空间
	// 输入文件rgb大小系: 391680; 而261120=480x272x2 = w x h x 2
	size_t yuv422_size = image.cols * image.rows * 2;
	printf("yuv422_size:%d\n", yuv422_size);
	__u8 *yuv422 = (__u8*)malloc(yuv422_size);
	memset(yuv422, 0, yuv422_size);
	// FIXME: 后缀名改为.yuyv, 文件能正常以彩色打开;
	// FIXME: 后缀名改为.yvyu, 文件打开偏蓝;
	RGB24TOYUV422(pbmp, image.cols, image.rows, yuv422);

	FILE *yuv_fd = NULL;
	fopen_s(&yuv_fd, opFn.c_str(), "wb");
	fwrite(yuv422, yuv422_size, 1, yuv_fd);
	fclose(yuv_fd);

	return 0;
}

int convertPng2Bgd(const char* filename)
{
	//// 2.文件名处理
	string fn(filename);
	size_t extPos = fn.find(".png");
	string extName = fn.substr(extPos);
	string pureName = fn.substr(0, fn.size() - extName.size());

	string opFn = pureName + ".bgd";
	Mat image = imread(filename);
	if (image.empty()) {
		std::cout << "Failed to load image file." << std::endl;
		return 1;
	}
	//// 2.构造rgb数据空间
	size_t png_size = image.rows * image.cols * 3;
	printf("png_size:%d\n", png_size);
	__u8 * pbmp = (__u8*)malloc(png_size);
	int k = 0;
	for (int row = 0; row < image.rows; row++) {
		for (int col = 0; col < image.cols; col++) {
			cv::Vec3b pixel = image.at<cv::Vec3b>(row, col);
			__u8 r = pixel[2];
			__u8 g = pixel[1];
			__u8 b = pixel[0];
			pbmp[k] = r;
			pbmp[k + 1] = g;
			pbmp[k + 2] = b;
			k += 3;
		}
	}
	//// 3.构建yuyv配置表和yuyv图像的存储空间
	// 输入文件rgb大小系: 391680; 而261120=480x272x2 = w x h x 2
	size_t yuv422_size = image.cols * image.rows * 2;
	printf("yuv422_size:%d\n", yuv422_size);
	size_t pyuyv_size = sizeof(__fb_file_header_t) + yuv422_size;
	__u8 *pyuyv = (__u8*)malloc(pyuyv_size);
	__u8 *yuv422 = pyuyv + sizeof(__fb_file_header_t);
	memset(yuv422, 0, yuv422_size);
	// FIXME: 后缀名改为.yuyv, 文件能正常以彩色打开;
	// FIXME: 后缀名改为.yvyu, 文件打开偏蓝;
	RGB24TOYUV422(pbmp, image.cols, image.rows, yuv422);
	free(pbmp);

	int w = image.cols;
	int h = image.rows;
	//// 4.先构造__fb_file_header_t
	__fb_file_header_t header;
	{
		__u8 header_raw[] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00, 0x20, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xDC, 0x05, 0x00, 0x00, 0xDC, 0x05, 0x00, 0x00, 0xDC, 0x05, 0x00, 0x54, 0x00, 0x00, 0x00, 0x54, 0xDC, 0x05, 0x00, 0x54, 0xB8, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		size_t header_size = sizeof(__fb_file_header_t);
		memcpy(&header, &header_raw, header_size);

		header.width = w;
		header.height = h;
		header.data_len[0] = w * h * 2 / 3; // 87,040; 384000 = 800 * 480 * 3
		header.data_len[1] = w * h * 2 / 3; // 87,040 = 800 * 480 * 3
		header.data_len[2] = w * h * 2 / 3;

		header.offset_data[0] = header_size;
		header.offset_data[1] = header_size + w * h * 2 / 3;
		header.offset_data[2] = header_size + (w * h * 2 / 3) * 2;
		memcpy(pyuyv, &header, header_size);
	}
	//// 4.1.构造props
	CLzmaEncProps props;
	props.level = 9;
	props.dictSize = (1 << 24);
	props.lc = 3;
	props.lp = 0;
	props.pb = 2;
	props.algo = 1;
	props.fb = 32;
	props.btMode = 1;
	props.numHashBytes = 4;
	props.mc = 32;
	props.writeEndMark = 0;
	props.numThreads = 2;
	size_t props_size = sizeof(props);

	//// 4.1.压缩
	__u8 *compressed_buf = (__u8 *)malloc(pyuyv_size);
	size_t compressed_size = pyuyv_size;	// sizeof(__fb_file_header_t) + yuv422_size;
	int ret = LzmaCompress(compressed_buf, &compressed_size,
		pyuyv, pyuyv_size,
		(__u8*)&props, &props_size,
		9, (1 << 24), 3, 0, 2, 32, 2);
	// props_size:5

	free(pyuyv);
	pyuyv = NULL;
	if (ret != 0 && ret != 7) {
		printf("compress err:%d\n", ret);
		free(compressed_buf);
		return -1;
	}
	//// 4.3.输出纯压缩文件
	{
		printf("calcCRC => %u\n", checkSum(compressed_buf, compressed_size));
		FILE * pureFile = NULL;
		string tmpStr = pureName + ".bin";
		fopen_s(&pureFile, tmpStr.c_str(), "wb");
		fwrite(compressed_buf, compressed_size, 1, pureFile);
		fclose(pureFile);
	}

	//// 5.构造整个文件的空间: 前8byte: 4B头; 4B压缩文件大小(高位高地址)
	size_t file_size = 8 + LZMA_PROPS_SIZE + compressed_size;
	__u8 *pfile = (__u8 *)malloc(file_size);
	__u8 *pcompressed_buf= (__u8 *)(pfile + 8) + LZMA_PROPS_SIZE;
	// FIXME:此处uncompress_size长度系: __fb_file_header_t + 未压缩数据长度
	uint32_t uncompress_size = pyuyv_size;
	const char magic_head[8] = {
		0x41, 0x5A, 0x31, 0x30,
		(uncompress_size & 0x000000FF)   ,
		(uncompress_size & 0x0000FF00) >> 8,
		(uncompress_size & 0x00FF0000) >> 16,
		(uncompress_size & 0xFF000000) >> 24
	};
	printf("compressed_size:%d\n", compressed_size);
	memcpy(pcompressed_buf, compressed_buf, compressed_size);
	memcpy((__u8 *)(pfile + 8), (__u8*)&props, props_size);
	memcpy(pfile, &magic_head, sizeof(magic_head));

	FILE *bgd_fd = NULL;
	fopen_s(&bgd_fd, opFn.c_str(), "wb");
	fwrite(pfile, file_size, 1, bgd_fd);
	fclose(bgd_fd);

	return 0;
}

int compressLZMA2Bgd(const char* filename)
{
	string fn(filename);
	size_t extPos = fn.find(".");
	string extName = fn.substr(extPos);
	string pureName = fn.substr(0, fn.size() - extName.size());
	string opFn = pureName + ".bgd";

	FILE *bmp_fd = NULL;
	fopen_s(&bmp_fd, filename, "rb");
	fseek(bmp_fd, 0, SEEK_END);
	size_t bmp_size = ftell(bmp_fd);
	fseek(bmp_fd, 0, SEEK_SET);
	__u8 * pbmp = (__u8*)malloc(bmp_size);
	fread_s(pbmp, bmp_size, bmp_size, 1, bmp_fd);
	fclose(bmp_fd);

	//// 4.1.构造props
	CLzmaEncProps props;
	size_t props_size = 5;

	//// 4.2.压缩
	__u8 *compressed_buf = (__u8 *)malloc(bmp_size);
	size_t compressed_size = bmp_size;	// 没啥用
	int ret = LzmaCompress(
		compressed_buf, &compressed_size,
		pbmp, bmp_size,
		(__u8*)&props, &props_size,
		9, (1 << 24), 3, 0, 2, 32, 2);
	free(pbmp);
	pbmp = NULL;
	if (ret != 0 && ret != 7) {
		printf("compress err:%d\n", ret);
		free(compressed_buf);
		return -1;
	}
	//// 5.构造整个文件的空间: 前8byte: 4B头; 4B压缩文件大小(高位高地址)
	size_t file_size = 8 + LZMA_PROPS_SIZE + compressed_size;
	__u8 *pfile = (__u8 *)malloc(file_size);
	__u8 *pcompressed_buf = (__u8 *)(pfile + 8) + LZMA_PROPS_SIZE;
	// FIXME:此处uncompress_size长度系: __fb_file_header_t + 未压缩数据长度
	uint32_t uncompress_size = bmp_size;
	const char magic_head[8] = {
		0x41, 0x5A, 0x31, 0x30,
		(uncompress_size & 0x000000FF)   ,
		(uncompress_size & 0x0000FF00) >> 8,
		(uncompress_size & 0x00FF0000) >> 16,
		(uncompress_size & 0xFF000000) >> 24
	};
	printf("compressed_size:%d\n", compressed_size);
	memcpy(pcompressed_buf, compressed_buf, compressed_size);
	memcpy((__u8 *)(pfile + 8), (__u8*)&props, props_size);
	memcpy(pfile, &magic_head, sizeof(magic_head));

	FILE *bgd_fd = NULL;
	fopen_s(&bgd_fd, opFn.c_str(), "wb");
	fwrite(pfile, file_size, 1, bgd_fd);
	fclose(bgd_fd);

	return 0;
}

int decompressLZMA(const char* filename, size_t originSize, __u8 *props_arr, size_t props_size)
{
	if (originSize == 0)
		return -1;

	//CLzmaEncProps props;
	//props.level = 9;
	//props.dictSize = (1 << 24);
	//props.lc = 3;
	//props.lp = 0;
	//props.pb = 2;
	//props.algo = 1;
	//props.fb = 32;
	//props.btMode = 1;
	//props.numHashBytes = 4;
	//props.mc = 32;
	//props.writeEndMark = 0;
	//props.numThreads = 2;
	//size_t props_size = sizeof(props);

	FILE *bmp_fd = NULL;
	fopen_s(&bmp_fd, filename, "rb");
	fseek(bmp_fd, 0, SEEK_END);
	size_t bmp_size = ftell(bmp_fd);
	fseek(bmp_fd, 0, SEEK_SET);
	__u8 * pbmp = (__u8*)malloc(bmp_size);
	fread_s(pbmp, bmp_size, bmp_size, 1, bmp_fd);
	fclose(bmp_fd);

	size_t dest_size = originSize;	// 90244 x 2 = byte < 261204 byte
	__u8 * dest = (__u8*)malloc(dest_size);
	int ret = LzmaUncompress(dest, &dest_size, pbmp, &bmp_size, props_arr, props_size);
	free((void*)props_arr);
	printf("LzmaUncompress => %d\n", ret);
	if (ret == 0) {
		FILE *fd = NULL;
		fopen_s(&fd, "extract.png", "wb");
		if (fd == NULL)
			perror("fd");
		ret = fwrite(dest, dest_size, 1, fd);
		fclose(fd);
	}
	return 0;
}

int decompressLZMAByRAM(__u8 *buf, size_t bufLen, size_t originSize, __u8 *props_arr, size_t props_size)
{
	__u8 * pbmp = buf;
	size_t bmp_size = bufLen;

	size_t dest_size = originSize;	// 90244 x 2 = byte < 261204 byte
	__u8 * dest = (__u8*)malloc(dest_size);
	// 若props_arr: 5D 00 00 01 00则会返回-1; 正确应该为5D 00 00 00 01
	int ret = LzmaUncompress(dest, &dest_size, pbmp, &bmp_size, props_arr, props_size);
	//free((void*)props_arr);
	printf("LzmaUncompress => %d\n", ret);
	if (ret == 0) {
		FILE *fd = NULL;
		fopen_s(&fd, "extract.bin", "wb");
		if (fd == NULL)
			perror("fd");
		ret = fwrite(dest, dest_size, 1, fd);
		fclose(fd);
	}
	return 0;
}

int testDecompress(const char* filename)
{
	//// 0.文件名处理
	string fn(filename);
	size_t extPos = fn.find(".png");
	string extName = fn.substr(extPos);
	string pureName = fn.substr(0, fn.size() - extName.size());
	string opFn = pureName + ".lzma";

	//// 1.先压缩
	//// 2.读取文件
	FILE *bmp_fd = NULL;
	fopen_s(&bmp_fd, filename, "rb");
	fseek(bmp_fd, 0, SEEK_END);
	size_t bmp_size = ftell(bmp_fd);
	fseek(bmp_fd, 0, SEEK_SET);
	__u8 * pbmp = (__u8*)malloc(bmp_size);
	fread_s(pbmp, bmp_size, bmp_size, 1, bmp_fd);
	fclose(bmp_fd);

	//// 4.1.构造props
	//CLzmaEncProps props;
	//props.level = 9;
	//props.dictSize = (1 << 24);
	//props.lc = 3;
	//props.lp = 0;
	//props.pb = 2;
	//props.algo = 1;
	//props.fb = 32;
	//props.btMode = 1;
	//props.numHashBytes = 4;
	//props.mc = 32;
	//props.writeEndMark = 0;
	//props.numThreads = 2;
	size_t props_size = sizeof(CLzmaEncProps);
	__u8 *pprops = (__u8*)malloc(props_size);

	//// 4.1.压缩
	__u8 *compressed_buf = (__u8 *)malloc(bmp_size);
	size_t compressed_size = bmp_size;	// sizeof(__fb_file_header_t) + yuv422_size;
	int ret = LzmaCompress(
		compressed_buf, &compressed_size,
		pbmp, bmp_size,
		pprops, &props_size,
		9, (1 << 24), 3, 0, 2, 32, 2);

	//// 5.输出文件
	FILE *bgd_fd = NULL;
	fopen_s(&bgd_fd, opFn.c_str(), "wb");
	fwrite(compressed_buf, compressed_size, 1, bgd_fd);
	fclose(bgd_fd);

	//// 6.再解压缩
	return decompressLZMA(opFn.c_str(), bmp_size, (__u8*)pprops, props_size);
}

int decompressLZMA2(const char* filename, size_t originSize)
{
	if (originSize == 0)
		return -1;

	//CLzmaEncProps props;
	//props.level = 9;
	//props.dictSize = (1 << 24);
	//props.lc = 3;
	//props.lp = 0;
	//props.pb = 2;
	//props.algo = 1;
	//props.fb = 32;
	//props.btMode = 1;
	//props.numHashBytes = 4;
	//props.mc = 32;
	//props.writeEndMark = 0;
	//props.numThreads = 2;
	//size_t props_size = sizeof(props);

	FILE *bmp_fd = NULL;
	fopen_s(&bmp_fd, filename, "rb");
	fseek(bmp_fd, 0, SEEK_END);
	size_t bmp_size = ftell(bmp_fd);
	fseek(bmp_fd, 0, SEEK_SET);
	__u8 * pbmp = (__u8*)malloc(bmp_size);
	fread_s(pbmp, bmp_size, bmp_size, 1, bmp_fd);
	fclose(bmp_fd);

	__u8 props_arr[5] = { 0x5D, 0,0,0, 0x01 };
	size_t props_size = 5;
	size_t dest_size = originSize;	// 90244 x 2 = byte < 261204 byte
	__u8 * dest = (__u8*)malloc(dest_size);
	int ret = LzmaUncompress(dest, &dest_size, pbmp, &bmp_size, props_arr, props_size);
	printf("LzmaUncompress => %d\n", ret);
	if (ret == 0) {
		FILE *fd = NULL;
		fopen_s(&fd, "extract.bin", "wb");
		if (fd == NULL)
			perror("fd");
		ret = fwrite(dest, dest_size, 1, fd);
		fclose(fd);
	}
	return 0;
}

int decompressBgd(const char* filename)
{
	//// 0.文件名处理
	//string fn(filename);


	//// 1.先压缩
	//// 2.读取文件
	FILE *bmp_fd = NULL;
	fopen_s(&bmp_fd, filename, "rb");
	fseek(bmp_fd, 0, SEEK_END);
	size_t bmp_size = ftell(bmp_fd);
	fseek(bmp_fd, 0, SEEK_SET);
	__u8 * pbmp = (__u8*)malloc(bmp_size);
	fread_s(pbmp, bmp_size, bmp_size, 1, bmp_fd);
	fclose(bmp_fd);
	
	size_t oriLen = 0;
	__u8 *ptr = pbmp + 4;
	for (size_t i = 0; i < 4; i++) {
		oriLen |= (ptr[i] << (i * 8));
	}
	__u8 *props = pbmp + 4 + 4;
	ptr = pbmp + 8 + 5;
	
	int ret = decompressLZMAByRAM(ptr, bmp_size - 8 - 5, oriLen, props, 5);
	if (ret != 0) {
		printf("decompressLZMAByRAM => %d\n", ret);
	}
	return ret;
}

void print_usage() {
	printf("aw_lzma (method) (args)\n");
	printf("1:convert png to yuyv. aw_lzma 1 xx.png => xx.yuyv\n");
	printf("2:convert png to opa. aw_lzma 2 xx.png => xx.bgd \n");
	printf("3:decompress bgd to fb_file_header and yuyv data. aw_lzma 3 xx.bgd => xx.bin\n");
	printf("4:compress the file to bgd. aw_lzma 4 xx.bin\n");
	printf("Author:YE 1004707812@qq.com\n");
}

int main(int argc, char* argv[])
{
	// 字节数据转__fb_file_header_t
	__fb_file_header_t header;
	__u8 header_raw[] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00, 0x20, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xDC, 0x05, 0x00, 0x00, 0xDC, 0x05, 0x00, 0x00, 0xDC, 0x05, 0x00, 0x54, 0x00, 0x00, 0x00, 0x54, 0xDC, 0x05, 0x00, 0x54, 0xB8, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	size_t header_size = sizeof(__fb_file_header_t);
	memcpy(&header, &header_raw, header_size);
	// 提取参数
	if (argc < 3) {
		print_usage();
		return 0;
	}
	int ret = 0;
	if (argc >= 3) {
		ret = atoi(argv[1]);
		switch (ret)
		{
		case 1:
			ret = convertPng2Yuyv(argv[2]);
			break;
		case 2:
			ret = convertPng2Bgd(argv[2]);
			break;
		case 3:
			ret = decompressBgd(argv[2]);
			break;
		case 4:
			ret = compressLZMA2Bgd(argv[2]);
			//ret = testDecompress(argv[2]);
			break;
		default:
			printf("unknown method.\n");
			print_usage();
			break;
		} 
	}
	return ret;
	
	// FIXME:需要指明src2的空间, 不然会ret=6, LZMA_STATUS_NEEDS_MORE_INPUT(dec.c:784)
	//size_t src2_size = sizeof(src2);
	//return 0;
}
